<template>
	<view class="device-bind-layout">

		<view class="device-bind-header">
			<text>点击</text>
			<image src="/static/home/home_scan_icon.png" mode="aspectFit"></image>
			<text>扫一扫，获取终端机具TUSN号</text>
		</view>

		<view class="device-bind-ksn-box">
			<xh-input placeholder="请输入终端机具的TUSN号" :isLine="false" padding="0" v-model="ksnNo"></xh-input>
			<image src="/static/home/home_scan_icon.png" mode="aspectFit" @tap="scanCode"></image>
		</view>
		<view class="submit-btn" style="margin-top: 128rpx;" @tap="submit">确认绑定</view>
		<!-- 示例提示信息 -->
		<view class="device-bind-tip">
			<text>扫码示例</text>
			<text>请扫码机具背面的条码</text>
		</view>
		<!-- 示例图 -->
		<view class="device-bind-barcode">
			<image src="/static/home/bindBarCode.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { remoteRegisterMerchantBindDevice } from '@/common/api/api.js'

	const props = defineProps(['merchantNo'])
	console.log(props.merchantNo)
	// 机具编号
	const ksnNo = ref('')
	// 扫码
	const scanCode = () => {
		uni.scanCode({
			// #ifdef APP-PLUS
			autoDecodeCharSet: true,
			//#endif
			success(res) {
				ksnNo.value = res.result
			}
		})
	}
	// 确认绑定
	const submit = async () => {
		const deviceNo = uni.$uv.trim(ksnNo.value, 'all')
		if (uni.$uv.test.empty(deviceNo)) {
			uni.$utils.showToast('请输入终端机具的TUSN号')
			return
		}
		let params = {}
		params.merchantNo = props.merchantNo
		params.ksnNo = deviceNo
		await remoteRegisterMerchantBindDevice(params)
		uni.$utils.showToast('终端绑定成功',1000,()=>{
			uni.$emit('reloadDeviceBindList')
			uni.navigateBack()
		})
	}
</script>

<style lang="scss" scoped>
	.device-bind-layout {
		padding: 30rpx;

		.device-bind-header {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;

			image {
				width: 28rpx;
				height: 28rpx;
				margin: 0 10rpx;
			}
		}

		.device-bind-ksn-box {
			display: flex;
			align-items: center;
			background-color: #fff;
			padding: 0 30rpx;
			margin-top: 30rpx;
			border-radius: 16rpx;

			image {
				width: 32rpx;
				height: 32rpx;
				margin-left: 20rpx;
			}
		}

		.device-bind-tip {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 48rpx 0;

			text:nth-child(2) {
				margin-top: 12rpx;
				color: $xh-color-9;
			}
		}

		.device-bind-barcode {
			background-color: #fff;
			padding: 30rpx;

			image {
				width: 100%;
			}
		}
	}
</style>